/**
 * 字体操作支持
 */
export const useFont = (
    config: {
        family: string;
        source: string | BinaryData;
        format?:
            | "embedded-opentype"
            | "truetype"
            | "opentype"
            | "woff"
            | "woff2"
            | "svg";
    } & FontFaceDescriptors
) => {
    const source =
        typeof config.source === "string"
            ? `url(${config.source}) ${
                  config.format ? `format('${config.format}')` : ""
              }`
            : config.source;
    const font = new FontFace(config.family, source, config);
    return {
        font,
        load() {
            return font.load().then(() => {
                document.fonts.add(font);
            });
        },
        unload() {
            return document.fonts.delete(font);
        },
    };
};
